package integraltesk

import (
	"context"
	"strings"

	"mall/common/errorx"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/common/utils/pointy"
	"mall/service/setting/rpc/settingclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateIntegralTaskLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreateIntegralTaskLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateIntegralTaskLogic {
	return &CreateIntegralTaskLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreateIntegralTaskLogic) CreateMemberTask(req *types.IntegralTaskInfo, ip string) (resp *types.BaseMsgResp, err error) {
	logx.Infof("CreateMemberTask : %+v ", req)
	if req.RuleName == nil || len(strings.TrimSpace(*req.RuleName)) == 0 {
		return nil, errorx.Error(errorx.ERROR_FORUM_POST_USER_ID_REQUIRED, "")
	}
	res, err := l.svcCtx.SettingRpc.BackendCreateIntegralTask(l.ctx, &settingclient.IntegralTaskInfo{
		RuleName:      req.RuleName,
		RuleSlug:      req.RuleSlug,
		RuleType:      req.RuleType,
		PicId:         req.PicId,
		PicUrl:        req.PicUrl,
		Frequency:     req.Frequency,
		Points:        req.Points,
		EffectiveTime: req.EffectiveTime,
		Ip:            pointy.GetPointer(ip),
		Description:   req.Description,
		Sort:          req.Sort,
		Status:        req.Status,
		DelStatus:     req.DelStatus,
		CreatedAt:     req.CreatedAt,
		UpdatedAt:     req.UpdatedAt,
		DeletedAt:     req.DeletedAt,
	})
	if err != nil {
		return nil, err
	}
	return &types.BaseMsgResp{
		Success: true,
		Msg:     l.svcCtx.Trans.Trans(l.ctx, res.Msg),
	}, nil
}
